﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DMS.DAL;

namespace DMS.DAL
{
    public class DALclass
    {
        #region global
        private DMSEntities dms = null;
        private static DALclass _instance = null;
        public static DALclass getInstance()
        {
            if (_instance == null)
                _instance = new DALclass();
            return _instance;
        }
        private DALclass()
        {
            dms = new DMSEntities();
        }
        #endregion
        //put you code here
        #region DocumentLibrary
        public int saveDocumentLibrary(DocumentLibrary dl)
        {
            if (dl.ID == 0)
                dms.AddToDocumentLibrary(dl);
            return dms.SaveChanges();
        }
        public DocumentLibrary getDocumentLibraryById(int documentLibraryId)
        {
            var query = from d in dms.DocumentLibrary
                        where d.ID == documentLibraryId
                        select d;
            if (query.Count() > 0)
                return query.First();
            else
                return null;
        }
        public List<DocumentLibrary> getAllDocumentLibrary()
        {
            var query = from d in dms.DocumentLibrary
                        orderby d.ID ascending
                        select d;
            if (query.Count() > 0)
                return query.ToList();
            else
                return null;
        }
        #endregion
        #region folders
        public List<Folder> getFoldersByLibrary(int libraryId, int current)
        {
            var query = from d in dms.Folder
                        where d.DocumentLibrary.ID == libraryId
                        && d.ID != current
                        && d.Folder2 ==null
                        select d;
            if (query.Count() > 0)
                return query.ToList();
            else
                return null;
        }
        public List<Folder> getFoldersByLibraryAndFolder(int libraryId, int folderId, int current)
        {
            var query = from d in dms.Folder
                        where d.DocumentLibrary.ID == libraryId
                        && d.ID != current
                        &&
                        (d.Folder2 != null
                        &&
                        d.Folder2.ID == folderId)
                        orderby d.Name
                        select d;
            if (query.Count() > 0)
                return query.ToList();
            else
                return null;
        }
 
        public Folder getFolderById(int folderId)
        {
            var query = from d in dms.Folder
                        where d.ID == folderId
                        select d;
            if (query.Count() > 0)
                return query.First();
            else
                return null;
        }
        public int saveFolder(Folder fldr)
        {
            if (fldr.ID == 0)
                dms.AddToFolder(fldr);
            return dms.SaveChanges();
        }
        #endregion
        #region User
        public User getUserById(int id)
        {
            var query = from d in dms.User
                        where d.ID == id
                        select d;
            if (query.Count() > 0)
                return query.First();
            else
                return null;
        }
        #endregion
        #region storage provider
        public StorageProvider getStorageProviderById(int id)
        {
            var query = from d in dms.StorageProvider
                        where d.ID == id
                        select d;
            if (query.Count() > 0)
                return query.First();
            else
                return null;
        }
        public List<StorageProvider> getAllStorageProvider()
        {
            var query = from d in dms.StorageProvider
                        orderby d.ID
                        select d;
            if (query.Count() > 0)
                return query.ToList();
            else
                return null;
        }
        #endregion
        #region Misc
        public void deleteObject(object obj)
        {
            dms.DeleteObject(obj);
            dms.SaveChanges();
        }
        #endregion
    }
}
